package org.gikolet.ch2.android.activity;

import java.util.ArrayList;
import java.util.List;

import org.gikolet.ch2.android.viewer.ActivityFeature;
import org.gikolet.ch2.android.viewer.ViewChangeListener;
import org.gikolet.ch2.android.viewer.Viewer;
import org.gikolet.ch2.android.widget.HorizontalScroller;

import roboguice.activity.GuiceActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;

public class HorizontalScrollerActivity extends GuiceActivity implements
		ActivityFeature {

	private static final String CURRENT_VIEW_INDEX_KEY = "CURRENT_VIEW_INDEX";

	private HorizontalScroller parent;
	private List<Viewer> viewers;

	private Menu menu;

	public HorizontalScrollerActivity() {
		this.viewers = new ArrayList<Viewer>();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.parent = new HorizontalScroller(this, null);

		this.parent.setViewChangeListener(new ViewChangeListener() {
			public void viewChanged(ViewGroup parent, View oldView, View newView) {
				createMenu();
				int index = HorizontalScrollerActivity.this.parent
						.getCurrentViewIndex();

				Viewer currentViewer = viewers.get(index);

				currentViewer.setFocus();
			}
		});

		for (Viewer v : this.viewers) {
			v.setActivityFeature(this);
			v.onCreate(savedInstanceState);
			v.createView(this.parent);
		}

		setContentView(this.parent);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);

		outState.putInt(CURRENT_VIEW_INDEX_KEY, this.parent
				.getCurrentViewIndex());

		for (Viewer v : this.viewers) {
			v.saveInstanceState(outState);
		}
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onSaveInstanceState(savedInstanceState);

		this.parent.setCurrentView(savedInstanceState.getInt(
				CURRENT_VIEW_INDEX_KEY, 0));

		for (Viewer v : this.viewers) {
			v.restoreInstanceState(savedInstanceState);
		}
	}

	public void addViewer(Viewer viewer) {
		this.viewers.add(viewer);

		viewer.setActivityFeature(this);
		viewer.onCreate(null);
		viewer.createView(this.parent);

		int index = this.parent.getChildCount() - 1;
		View view = this.parent.getChildAt(index);

		if (index == this.parent.getCurrentViewIndex()) {
			view.setFocusable(true);
		} else {
			view.setFocusable(false);
		}
	}

	private void createMenu() {
		if (this.menu != null) {
			this.menu.clear();

			int index = this.parent.getCurrentViewIndex();
			if (0 <= index && index < this.viewers.size()) {
				this.viewers.get(index).createMenu(menu);
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		this.menu = menu;

		createMenu();
		return true;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		int index = this.parent.getCurrentViewIndex();
		if (index != HorizontalScroller.INVALID_VIEW_INDEX) {
			viewers.get(index).createContextMenu(menu, v, menuInfo);
			return;
		}
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int index = this.parent.getCurrentViewIndex();
		if (index != HorizontalScroller.INVALID_VIEW_INDEX) {
			return viewers.get(index).onMenuItemSelected(item);
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		int index = this.parent.getCurrentViewIndex();
		if (index != HorizontalScroller.INVALID_VIEW_INDEX) {
			return viewers.get(index).onContextItemSelected(item);
		}

		return super.onContextItemSelected(item);
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent e) {
		int index = this.parent.getCurrentViewIndex();
		if (index != HorizontalScroller.INVALID_VIEW_INDEX) {
			return viewers.get(index).dispatchKeyEvent(e);
		}

		return super.dispatchKeyEvent(e);
	}

	public void requestView(Viewer viewer) {
		int index = this.viewers.indexOf(viewer);
		if (index != -1) {
			this.parent.setCurrentView(index);
		}
	}
}